package com.ubi.core.framework.exceptionhandler;

import com.ubi.core.framework.exception.BatchDeleteException;
import com.ubi.core.framework.exception.BusinessException;
import com.ubi.core.framework.exception.KickException;
import com.ubi.core.framework.exception.ResourceNotFoundException;
import com.ubi.core.framework.exception.UploadFailException;
import com.ubi.core.framework.exception.UserAuthorizationException;
import com.ubi.core.framework.response.ErrorResponse;
import com.ubi.core.framework.response.ErrorResponseBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.io.IOException;

@ControllerAdvice
public class GlobalExceptionHandler {

    @Autowired
    ErrorResponseBuilder responseBuilder;

    @ExceptionHandler(UserAuthorizationException.class)
    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public ErrorResponse handleUserAuthorizationException(UserAuthorizationException exception) {
        return responseBuilder.buildAuthenticationError(exception, HttpStatus.UNAUTHORIZED);
    }

    @ExceptionHandler(KickException.class)
    @ResponseStatus(value = HttpStatus.GONE)
    @ResponseBody
    public ErrorResponse handleKickException(KickException exception) {
        return responseBuilder.buildKickExceptionResponse(exception);
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ErrorResponse handleException(Exception ex) {
        return responseBuilder.build(ex, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public ErrorResponse handleBusinessException(BusinessException exception) {
        return responseBuilder.buildBusinessExceptionResponse(exception);
    }

    @ExceptionHandler(BatchDeleteException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ErrorResponse handBatchDeleteException(BatchDeleteException batchException) {
        return responseBuilder.buildBatchDeleteExceptionResponse(batchException);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ErrorResponse handleValidationException(MethodArgumentNotValidException ex) throws IOException {
        return responseBuilder.buildValidationExceptionResponse(ex);
    }

    @ExceptionHandler(ResourceNotFoundException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ErrorResponse handleResourceNotFoundException(ResourceNotFoundException resourceNotFoundException) {
        return responseBuilder.buildResourceNotFoundException(resourceNotFoundException);
    }

    @ExceptionHandler(UploadFailException.class)
    @ResponseStatus(value = HttpStatus.OK)
    @ResponseBody
    public ErrorResponse handleUploadFailException(UploadFailException exception) {
        return responseBuilder.buildUploadFailExceptionResponse(exception);
    }
}
